package cn.net.isir.common;

import cn.net.isir.core.JedisManager;
import cn.net.isir.core.SpringApplicationContextUtils;
import cn.net.isir.entity.WxMinipUserinfo;
import cn.net.isir.service2.WxMinipUserinfoService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * @author liuxubo
 * @filename UserCacheManager.java
 * @package cn.net.isir.common
 * @email lxb@isir.net.cn
 * @date 2018/09/02
 * @comment
 * @since 1.7
 */
public class CacheManager {

    private static JedisManager jedisManager = SpringApplicationContextUtils.getBean("jedisManager", JedisManager.class);
    private static WxMinipUserinfoService wxMinipUserinfoService = SpringApplicationContextUtils.getBean("wxMinipUserinfoService", WxMinipUserinfoService.class);

    /**
     * 获取微信用户信息
     *
     * @param unionid unionid
     * @return 用户信息
     */
    public static WxMinipUserinfo getWxUserInfo(String unionid) {
        WxMinipUserinfo info = new WxMinipUserinfo();
        info.setUnionid(unionid);
        return wxMinipUserinfoService.selectOne(info);
    }

    public static void setOpenidSessionKey(String openid, String session_key) {
        jedisManager.set("wxminip-sessionKey-openid-" + openid, session_key);
    }

    public static String getOpenidSessionKey(String openid) {
        return jedisManager.get("wxminip-sessionKey-openid-" + openid);
    }

    public static void setUnionidSessionKey(String unionid, String session_key) {
        jedisManager.set("wxminip-sessionKey-unionid-" + unionid, session_key);
    }

    public static String getUnionidSessionKey(String unionid) {
        return jedisManager.get("wxminip-sessionKey-unionid-" + unionid);
    }

    /**
     * 保存短信验证码
     *
     * @param mobile  手机号
     * @param verCode 验证码
     * @param expire  过期时间(单位:秒)
     */
    public static void setVerCode(String mobile, String type, String verCode, int expire) {
        set("VerCode-" + type + "-mobile-" + mobile, verCode);
        if (expire > 0) {
            expire("VerCode-" + type + "-mobile-" + mobile, expire);
        }
    }

    /**
     * 获取验证码
     *
     * @param mobile 手机号
     */
    public static String getVerCode(String mobile, String type) {
        return get("VerCode-" + type + "-mobile-" + mobile);
    }

    /**
     * 删除验证码
     *
     * @param mobile 手机号
     */
    public static void deleteVerCode(String mobile, String type) {
        delete("VerCode-" + type + "-mobile-" + mobile);
    }

    /**
     * 新增ticket(过期时间五分钟)
     *
     * @param ticket ticket令牌
     * @param obj    对象
     */
    public static void setTicket(String ticket, Object obj) {
        set("ticket-" + ticket, JSON.toJSONString(obj));
        expire("ticket-" + ticket, 5 * 60);
    }

    /**
     * @param ticket ticket令牌
     * @return 对象的json字符串
     */
    public static String getTicket(String ticket) {
        return get("ticket-" + ticket);
    }

    /**
     * 设置缓存
     *
     * @param key   key
     * @param value value
     */
    public static void set(String key, String value) {
        jedisManager.set(key, value);
    }

    /**
     * 获取缓存
     *
     * @param key key
     * @return value
     */
    public static String get(String key) {
        return jedisManager.get(key);
    }

    /**
     * 设置过期时间(单位:秒)
     *
     * @param key     key
     * @param seconds 过期时间(单位:秒),传入null或者0，则用不过期
     */
    public static void expire(String key, int seconds) {
        if (seconds != 0) {
            jedisManager.expire(key, seconds);
        }
    }

    /**
     * 移除key对应的redis缓存
     *
     * @param key key
     */
    public static void delete(String key) {
        if (get(key) != null) {
            jedisManager.delete(key);
        }
    }

    public static void main(String[] args) {
    }
}
